home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / pine3.07 / c-client / Internal.DOC < prev    next >
Encoding:
Text File  |  1991-12-15  |  35.2 KB  |  775 lines

  1.       Documentation of C-Client Functions and Interfaces
  2.  
  3.                  Mark Crispin
  4.             Revised 15 December 1991
  5.  
  6.                  Introduction
  7.  
  8.      The C-Client was originally written at Stanford University as a
  9. set of routines to support IMAP and SMTP from a main program which
  10. would handle the user interface.  A sample main program which uses the
  11. C-Client, MTEST.C, has run on a wide variety of platforms including
  12. Unix, TOPS-20, Macintosh, and MS-DOS.
  13.  
  14.      This is a major redesign of the original C-Client, made at the
  15. University of Washington.  It uses ANSI C calling conventions
  16. throughout to assist in function argument type checking.  It also
  17. provides the capability of having multiple means of accessing mail
  18. through the user of "drivers".  A future version of the C-Client will
  19. probably convert the current direct usage of SMTP to a driver as well.
  20.  
  21.      The most important file for the author of an application using
  22. the C-Client is MAIL.H.  MAIL.H defines several important structures
  23. of data which are passed between the main program and the C-Client.
  24. Although some functions (e.g. mail_fetchtext()) return the data
  25. fetched as a convenience, for some data items (e.g. flags) you need to
  26. get the data as a structure reference.  MAIL.H also defines a number
  27. of useful constants.
  28.  
  29.      It should be noted that when MAIL functions exist to reference
  30. data they should be used in preference to referencing the structures
  31. directly.  This is because in some cases the data is not actually
  32. fetched until a reference (via the function call) is made.  For
  33. example, although the MESSAGECACHE item for a message can be obtained
  34. by indexing messagarray, there is no guarantee that the item in fact
  35. exists unless mail_fetchenvelope() is called for that message.  Less
  36. costly functions, mail_fetchfast() and mail_fetchflags(), also exist
  37. to create and load a MESSAGECACHE item, but their usage is not
  38. recommended.
  39.  
  40.      The main program will probably also need to include SMTP.H,
  41. MISC.H, and OSDEP.H, but this usage should be solely to receive
  42. function prototypes.  Any other definitions in those files should be
  43. considered private to that module.
  44.  
  45.                I. Public interface
  46.  
  47.      The files listed in this section are the standard C-Client
  48. mailsystem library modules, and are operating system and machine
  49. independent.  Only those functions suitable for external callers are
  50. documented here; other functions are for internal use only and are
  51. used by external callers at their own risk!
  52.  
  53. [MAIL.C]:    Mail Access functions
  54.  
  55. [Note!!  There is an important difference between a "sequence" and a
  56.  "msgno".  A sequence is a string representing one or more messages in
  57.  IMAP2-style sequence format ("n", "n:m", or combination of these delimited
  58.  by commas), whereas a msgno is an int representing a single message.]
  59.  
  60. void mail_link (DRIVER *driver)
  61.  This function adds the specified driver to the list of mail drivers.
  62. Initially there are no drivers, so all programs which intend to use
  63. the MAIL routines need to have at least one call to this function.  A
  64. function which uses IMAP2 would have a statement such as:
  65.   mail_link (&imapdriver);    /* link in IMAP driver */
  66. early in the program's initialization.
  67.  
  68. void mail_find (MAILSTREAM *stream,char *pat);
  69.  This function finds known mailboxes that match the given wildcard
  70. pattern.  The application's mm_mailbox() function is called for each
  71. matching mailbox.
  72.  
  73. void mail_find_bboards (MAILSTREAM *stream,char *pat);
  74.  This function finds known bboards that match the given wildcard
  75. pattern.  The application's mm_bboard() function is called for each
  76. matching mailbox.
  77.  
  78. MAILSTREAM *mail_open (MAILSTREAM *oldstream,char *name,int debug)
  79.  This function opens the mailbox and if successful returns a stream
  80. suitable for use by the other MAIL functions.  If oldstream is
  81. non-NIL, an attempt is made to reuse oldstream as the stream for this
  82. mailbox.  If debug is non-NIL, the stream is set for debugging.  NIL
  83. is returned if this function fails for any reason.
  84.  
  85. MAILSTREAM *mail_close (MAILSTREAM *stream)
  86.  This function closes the MAIL stream and frees all resources
  87. associated with it that it may have created (subject to any handles
  88. existing).
  89.  
  90. MAILHANDLE *mail_makehandle (MAILSTREAM *stream)
  91.  This function creates and returns a handle to the stream, suitable
  92. for use as a secondary copy of the stream.  This is useful when some
  93. entity that wishes to access the stream may survive the stream without
  94. knowing that it outlived it.  For example, an object reading a message
  95. may have a handle to a stream, but the message selection object that
  96. spawned it (and which owns the stream) may have gone away.  A stream
  97. can be closed or recycled while handles are pointing at it, but it is
  98. not completely freed until all handles are gone.  A stream may have an
  99. arbitrary number of handles.
  100.  
  101. void mail_free_handle (MAILHANDLE **handle)
  102.  This function frees the handle and notifies the stream that it has
  103. one fewer handle.  If this is the last handle on the stream and the
  104. stream has been closed, then the stream is freed.
  105.  
  106. MAILSTREAM *mail_stream (MAILHANDLE *handle)
  107.  This function returns the stream associated with the handle if and
  108. only if the stream still represents the same MAIL connection associated
  109. with the handle.  Otherwise, NIL is returned (meaning that there is no
  110. active stream associated with this handle).
  111.  
  112. void mail_fetchfast (MAILSTREAM *stream,char *sequence)
  113.  This function causes a fetch of all the "fast" information (internal
  114. date, RFC 822 size, and flags) for the given sequence.  Since all this
  115. information is also fetched by mail_fetchenvelope(), this function is
  116. generally not used.
  117.  
  118. void mail_fetchflags (MAILSTREAM *stream,char *sequence)
  119.  This function causes a fetch of the flags for the given sequence.
  120. This main reason for using this function is to update the flags in the
  121. local cache in case some other process changed the flags (multiple
  122. simultaneous write access is allowed to the flags) as part of a "check
  123. entire mailbox" (as opposed to "check for new messages") operation.
  124.  
  125. ENVELOPE *mail_fetchenvelope (MAILSTREAM *stream,int msgno)
  126.  This function causes a fetch of all the information (envelope,
  127. internal date, RFC 822 size, flags, and body structure) for the given
  128. msgno and in the case of IMAP up to MAPLOOKAHEAD (a parameter in
  129. IMAP2.H) subsequent messages which are not yet in the cache.  No fetch
  130. is done if the envelope for the given msgno is already in the cache.
  131. The ENVELOPE for this msgno is returned.
  132.  This is the primary function for fetching non-text information about
  133. messages, and should be called before any attempt to reference cache
  134. information about this message via mail_elt().
  135.  After this function has been called the message body structure
  136. information can be obtained by using: mail_elt (stream,msgno)->body
  137.  
  138. char *mail_fetchheader (MAILSTREAM *stream,int msgno)
  139.  This function causes a fetch of the complete, unfiltered RFC 822
  140. format header of the specified message as a text string and returns
  141. that text string.
  142.  
  143. char *mail_fetchtext (MAILSTREAM *stream,int msgno)
  144.  This function causes a fetch of the non-header text of the specified
  145. message as a text string and returns that text string.  No attempt is
  146. made to segregate individual body parts.
  147.  
  148. char *mail_fetchbody (MAILSTREAM *stream,int m,char *sec,unsigned long *len)
  149.  This function causes a fetch of the particular section of the body of
  150. the specified message as a text string and returns that text string.
  151. The section specification is a string of integers delimited by period
  152. which index into a body part list as per the IMAP body parts extension
  153. RFC.  The length of the body part, which may not necessarily be
  154. null-terminated, is returned in len.  Body parts are not decoded by
  155. this function; see rfc822_base64() and rfc822_quotedprintable().
  156.  
  157. char *mail_fetchfrom (MAILSTREAM *stream,int msgno,int length)
  158.  This function returns a "from" string of the specified length for the
  159. specified message, suitable for display to the user in a menu line.
  160.  If the personal name of the first address in the envelope's from item
  161. is non-NIL, it is used; otherwise a string is created by appending the
  162. mailbox of the first address, an "@", and the host of the first
  163. address.  The string is trimmed or padded with trailing spaces as
  164. necessary to make its length match what the caller requested.
  165.  The length argument is provided to make it convenient for the
  166. application to decide upon the length it wants.  It should be
  167. considered a constant, since once generated the string is kept in the
  168. cache and is returned on subsequent calls instead of recomputed.
  169.  
  170. char *mail_fetchsubject (MAILSTREAM *stream,int msgno,int length)
  171.  This function returns a "subject" string of the specified length for
  172. the specified message, suitable for display to the user in a menu
  173. line.
  174.  The envelope's subject item is copied and trimmed as necessary to
  175. make its length be no more what the caller requested.  Unlike
  176. mail_fetchfromstring(), this function can return a string of shorter
  177. length than what the caller requested.
  178.  The warning about the length argument in mail_fetchfromstring() also
  179. applies to this function.
  180.  
  181. MESSAGECACHE *mail_elt (MAILSTREAM *stream,int msgno)
  182.  This function returns the cache entry for the specified message, and
  183. is preferred over using stream->messagearray[msgno-1].
  184.  Although this function will create a cache entry if it does not
  185. already exist, that functionality is for internal use only.  This
  186. function should never be called without having first called
  187. mail_fetchenvelope() on the message first.
  188.  
  189. void mail_setflag (MAILSTREAM *stream,char *sequence,char *flag)
  190.  This function causes a store to add the specified flag to the flags
  191. set for the messages in the specified sequence.  If there is any
  192. problem in setting flags, a message will be passed to the application
  193. via the mm_log() facility.
  194.  
  195. void mail_clearflag (MAILSTREAM *stream,char *sequence,char *flag)
  196.  This function causes a store to delete the specified flag from the
  197. flags set for the messages in the specified sequence.  If there is any
  198. problem in clearing flags, a message will be passed to the application
  199. via the mm_log() facility.
  200.  
  201. void mail_search (MAILSTREAM *stream,char *criteria)
  202.  This function causes a mailbox search using the specified criteria
  203. (in IMAP2 format).  The application's mm_searched() function is called
  204. for each message that matches the search criteria.  If there is any
  205. problem in searching, a message will be passed to the application via
  206. the mm_log() facility.
  207.  
  208. int mail_ping (MAILSTREAM *stream)
  209.  The function pings the stream to see if it is still active.  It may
  210. cause an "implicit check" for new mail.  It returns T if the stream is
  211. still alive, NIL otherwise.
  212.  
  213. void mail_check (MAILSTREAM *stream)
  214.  This function causes an explicit check of the mailbox for new
  215. messages including a remote reparse of all flags in the mailbox.  The
  216. application's mm_exists() function is called with the number of
  217. messages in the mailbox.  The status of the check is passed to the
  218. application via the mm_log() facility.
  219.  Note that mail_fetchflags() needs to be called to update the flags in
  220. the local cache.
  221.  
  222. void mail_expunge (MAILSTREAM *stream)
  223.  This function causes an expunge of the mailbox.  The application's
  224. mm_expunged() function is called for each message that has been
  225. expunged.  The application's mm_exists() function is called at the
  226. start and end of the expunge to ensure synchronization.  The status of
  227. the expunge is passed to the application via the mm_log() facility.
  228.  Note that the decrementing of msgno's for subsequent messages happens
  229. immediately; for example, if three consequtive messages starting at
  230. msgno 5 are expunged, mm_expunged() will be called with a msgno of 5
  231. three times.
  232.  
  233. int mail_copy (MAILSTREAM *stream,char *sequence,char *mailbox)
  234.  This function causes the messages in the specified sequence to be
  235. copied to the specified mailbox.  The system \Seen flag is set for
  236. these messages and T is returned if the copy is successful.
  237.  If there is any problem in copying, a message will be passed to the
  238. application via the mm_log() facility and the function returns NIL.
  239.  Note that the mailbox must be on the same host as the stream and is a
  240. mailbox only.
  241.  
  242. int mail_move (MAILSTREAM *stream,char *sequence,char *mailbox)
  243.  This function causes the messages in the specified sequence to be
  244. copied to the specified mailbox.  The system \Seen and \Deleted flags
  245. are set for these messages and T is returned if the copy is successful.
  246.  If there is any problem in copying, a message will be passed to the
  247. application via the mm_log() facility and the function returns NIL.
  248.  Note that the mailbox must be on the same host as the stream and is a
  249. mailbox only.
  250.  
  251. void mail_debug (MAILSTREAM *stream)
  252.  This function enables telemetry logging for this stream.  All
  253. telemetry is passed to the application via the mm_dlog() facility.
  254.  
  255. void mail_nodebug (MAILSTREAM *stream)
  256.  This function disables telemetry logging for this stream.
  257.  
  258. ENVELOPE *mail_newenvelope ()
  259.  This function returns a new, empty envelope.
  260.  
  261. ADDRESS *mail_newaddr ()
  262.  This function returns a new, empty address.
  263.  
  264. BODY *mail_newbody ()
  265.  This function returns a new, empty body.
  266.  
  267. PART *mail_newbody_part ()
  268.  This function returns a new, empty body part.
  269.  
  270. void mail_free_body (BODY **body)
  271.  This function frees a body and all its contents.
  272.  
  273. void mail_free_body_part (PART **part)
  274.  This function frees a body part and all its contents.
  275.  
  276. void mail_free_elt (MESSAGECACHE **elt)
  277.  This function frees a cache element.  Normally, this is used only if
  278. the main program has a private pointer to cache elements.  If so, it
  279. is expected to increment the cache element's lockcount when it makes a
  280. private pointer, and to call this function when it is finished with it.
  281.  
  282. void mail_free_envelope (ENVELOPE **env)
  283.  This function frees an envelope and all its contents.
  284.  
  285. void mail_free_address (ADDRESS **address)
  286.  This function frees an address and all its contents.
  287.  
  288. [MISC.C]:    Miscellaneous utility functions
  289.  
  290. char *ucase (char *string)
  291.  This function converts each lowercase character of the specified
  292. string to uppercase and returns the string.
  293.  
  294. char *lcase (char *string)
  295.  This function converts each uppercase character of the specified
  296. string to lowercase and returns the string.
  297.  
  298. char *cpystr (char *string);
  299.  This function returns a copy of the string.
  300.  
  301. int find_rightmost_bit (long *valptr)
  302.  This function returns -1 if the 32-bit value pointed to by valptr is
  303. non-zero, otherwise it returns the bit number (0 = LSB, 31 = MSB) of
  304. the right-most bit in that value.  This is used to convert from the
  305. bits in the cache's userFlags item to an index into the stream's
  306. userFlags array of flag texts.
  307.  
  308. long min (long i,long j)
  309.  This function returns the minimum of the two integers.
  310.  
  311. long max (long i,long j)
  312.  This function returns the maximum of the two integers.
  313.  
  314. int search (char *s,int c,char *pat,int patc)
  315.  This function does a fast case-independent search for the given
  316. pattern in pat (lenth patc) in base string s, and returns T if the
  317. pattern is found in the string.
  318.  
  319. [SMTP.C]:    Mail Transfer Protocol functions
  320.  
  321. SMTPSTREAM *mtp_open (char **hostlist,int debug)
  322.  This function opens an MTP connection to a one of the hosts in the
  323. host list and if successful returns a stream suitable for use by the
  324. other MTP functions.  The hosts are tried in order until a connection
  325. is successfully opened.  If debug is non-NIL, mtp_debug() is called on
  326. the stream immediately after the TCP connection is opened so that the
  327. greeting and hello protocol goes into the debugging telemetry.  NIL is
  328. returned if this function fails to open a connection to any of the
  329. hosts in the list.
  330.  
  331. void mtp_close (SMTPSTREAM *stream)
  332.  This function closes the MTP stream and frees all resources
  333. associated with it that it may have created.
  334.  
  335. int mtp_mail (SMTPSTREAM *stream,char *type,ENVELOPE *msg,BODY *body)
  336.  This function negotiates an MTP transaction of the specified type
  337. (one of "MAIL", "SEND", "SAML", or "SOML") to deliver the specified
  338. message.  This function returns T if success or NIL if there is any
  339. failure.  The text reason for the failure is in the stream's reply
  340. item; if it is associated with a recipient it is also in that address'
  341. error item.
  342.  
  343. MTPADR *mtp_parse_address (char *string,char *defaulthost)
  344.  This function parses a string that contains an address list and
  345. returns an address list suitable for the to, cc, or bcc items of a
  346. message.  The address list should be freed via mtp_free_address() when
  347. the application is finished with it.  If there are any parsing errors
  348. a message is passed to the application via the mm_log() facility.
  349.  
  350. void mtp_debug (SMTPSTREAM *stream)
  351.  This function enables SMTP protocol telemetry logging for this
  352. stream.  All SMTP protocol operations are passed to the application
  353. via the mm_dlog() facility.
  354.  
  355. void mtp_nodebug (SMTPSTREAM *stream)
  356.  This function disables SMTP protocol telemetry logging for this
  357. stream.
  358.  
  359. [RFC822.C]:    RFC 822 Protocol support functions
  360.  
  361. [Note: direct usage of the functions in this module is strongly discoraged,
  362.  since it creates a dependency upon RFC 822.]
  363.  
  364. void rfc822_header (char *header,ENVELOPE *env,BODY *body)
  365.  This function writes an RFC 822 format header into header based on
  366. the information in message.
  367.  
  368. void rfc822_write_address (char *destination,ADDRESS *address)
  369.  This function writes an RFC 822 format address list into destination
  370. based on the information in address and is therefore the inverse of
  371. mtp_parse_address().
  372.  
  373. void rfc822_parse_msg (ENVELOPE **en,BODY **bdy,char *s,unsigned long i,
  374.                char *b,unsigned long j,char *host,char *tmp)
  375.  This function parses the RFC 822 header pointed to by s with count i
  376. and body pointed to by b with count j into the specified destination
  377. envelope and body pointers, using host as the default host name and
  378. tmp as a scratch buffer.  This function is intended solely to support
  379. header parsing for non-IMAP MAIL drivers.
  380.  
  381. void rfc822_parse_adrlist (ADDRESS **lst,char *string,char *host)
  382.  This function is similar to mtp_parse_address(), except that the
  383. destination list is one of the arguments.  If the destination list is
  384. non-empty it appends the new addresses to the list.
  385.  
  386. void *rfc822_base64 (char *src,unsigned long srcl,unsigned long *len)
  387.  This function decodes a BASE64 body part given a source string and
  388. its length.  The decoded body part as a sequence of binary bytes is
  389. returned, and its length is returned in len.
  390.  
  391. char *rfc822_quotedprintable (char *src,unsigned long srcl,unsigned long *len)
  392.  This function decodes a Quoted-Printable body part given a source
  393. string and its length.  The decoded body part as an 8-bit character
  394. string is returned, and its length is returned in len.
  395.  
  396.              II. Main program co-routines
  397.  
  398.      All main programs which use the C-Client must have the following
  399. service co-routines:
  400.  
  401. [Main program]
  402.  
  403. #include "mail.h"
  404. #include "smtp.h"
  405. #include "misc.h"
  406. #include "osdep.h"
  407.  
  408. void mm_searched (MAILSTREAM *stream,int number)
  409.  This function is called from mail_search to notify the main program
  410. that this message number matches the search.  Note that the stream is
  411. locked and that therefore you cannot call any mail_xxx functions!
  412.  
  413. void mm_exists (MAILSTREAM *stream,int number)
  414.  This function is called from several functions to notify the main
  415. program that there are this many messages in the mailbox.  It is also
  416. used to notify the main program of new mail, by announcing a higher
  417. number than the main program was previously aware.  Note that the
  418. stream is locked and that therefore you cannot call any mail_xxx
  419. functions!
  420.  
  421. void mm_expunged (MAILSTREAM *stream,int number)
  422.  This function is called from mail_expunge to notify the main program
  423. that this message number has been expunged from the mail file and that
  424. all subsequent messages are now referenced by a message number one
  425. less than before.  This implicitly decrements the number of messages
  426. in the mailbox.  Note that the stream is locked and that therefore you
  427. cannot call any mail_xxx functions!
  428.  
  429. void mm_mailbox (char *string)
  430.  This function is called from mail_find to notify the main program
  431. that this mailbox matches the find request.  Note that the stream is
  432. locked and that therefore you cannot call any mail_xxx functions!
  433.  
  434. void mm_bboard (char *string)
  435.  This function is called from mail_find_bboards to notify the main
  436. program that this bboard matches the find request.  Note that the
  437. stream is locked and that therefore you cannot call any mail_xxx
  438. functions!
  439.  
  440. void mm_notify (MAILSTREAM *stream,char *string,int errflg)
  441.  This function is called when an unsolicited response of type OK, NO,
  442. or BAD from a remote IMAP server is received.  No newline is included
  443. in the string, so this function has to output its own.  The IMAP
  444. protocol defines this facility as a means for the server to transmit
  445. text for the end user to see.  Although the behavior of all possible
  446. IMAP server implementations can not be predicted, the following
  447. general assumptions can be made about the value of errflg and how it
  448. relates to the text based on how the c-client based IMAP server
  449. behaves:
  450.  NIL    normal operation.  The text is `babble' that the server
  451.     thinks may be interesting to the user.  Examples: the
  452.     greeting message from the server, notice of enabling of the
  453.     kludge to support MacMS, error in parsing the header of a
  454.     message in the remote mailbox (a c-client PARSE mm_log()
  455.     event).  Display of these messages to the user is optional.
  456.  WARN    A c-client WARN mm_log() event which occurred in the server.
  457.     See mm_log() documentation below.  This is also called from
  458.     the Berkeley mail driver if the requested mailbox write
  459.     operation failed because the main program returned NIL from
  460.     a "non-serious" mm_diskerror() call.
  461.  ERROR    An unknown IMAP protocol error, or an extremely serious
  462.     internal error.  These are supposedly impossible in debugged
  463.     software, and so should be called to the attention of the user
  464.     and/or support staff.  Whatever happened has probably already
  465.     disrupted the user's work, so it is alright to deal with this
  466.     in a modal fashion.
  467. Note that usually the stream is locked and that therefore you cannot
  468. call any mail_xxx functions!
  469.  
  470. void mm_log (char *string,int errflg)
  471.  This function is called on various c-client events.  No newline is
  472. included in the string, so this function has to output its own.  In
  473. general, it is intended that these be messages for the user to see.
  474. The value of errflg indicates the urgency of these messages, as
  475. follows:
  476.  NIL    normal operation.  The text is `babble' that the c-client
  477.     thinks may be interesting to the user.  Typically, these are
  478.     acknowledgement texts from various operations ("Expunged 3
  479.     messages", "Re-using connection", "Connection closing") which
  480.     may contain useful information.  Display of these messages to
  481.     the user is optional depending upon the needs of the
  482.     application.
  483.  PARSE    an error occurred in RFC 822 parsing.  Since bogus headers are
  484.     an all-too-common occurrence in the real word, these can often
  485.     be ignored on the GIGO principle.  However, it may be a good
  486.     idea to report these errors if calling the rfc822 routines as
  487.     part of command parsing e.g. mtp_parse_address().
  488.  WARN    an error condition occurred that did not abort the requested
  489.     operation, or which has a well-defined "right thing to do"
  490.     when it happens.  This can be things such as "Can't open
  491.     mailbox for read-write, so opening read-only", "Empty
  492.     mailbox", "Login failed, try again", "Waiting for mailbox to
  493.     become unlocked", etc.  Warnings can also occur if the abort
  494.     option is taken from mm_diskerror() to verify that the abort
  495.     has taken place.  Furthermore, it can occur (in massive
  496.     quantities!) in the event of a c-client detected IMAP
  497.     protocol error; but this should never happen in debugged
  498.     software.  Warnings should be called to the user's attention,
  499.     but probably should not interrupt the flow of the user's work
  500.     (that is, it's alright to display warnings in a view area
  501.     without waiting for any user acknowledgement of seeing the
  502.     warning).  It is not, however, absolutely necessary for an
  503.     application to display warnings.
  504.  ERROR    a serious error condition occured that aborted the requested
  505.     operation and possibly also aborted the mail stream.  This
  506.     ranges from normal error conditions such as "Can't open
  507.     mailbox", "too many login failures, go away" to bizarre
  508.     conditions such as "Apparent new mail appeared in the mailbox
  509.     that doesn't look like mail, program aborting".  Errors must
  510.     be called to the user's attention, and probably should require
  511.     some sort of acknowledgement (e.g. answering a modal panel)
  512.     before the application proceeds.
  513. Note that usually the stream is locked and that therefore you cannot
  514. call any mail_xxx functions!
  515.  
  516. void mm_dlog (char *string)
  517.  This function is called from several functions to output a string to
  518. a debugging telemetry stream.  No newline is included in the string,
  519. so this function has to output its own.  This is called only when
  520. debugging is enabled on the stream in question.  Note that usually the
  521. stream is locked and that therefore you cannot call any mail_xxx
  522. functions!
  523.  
  524. void mm_login (char *host,char *user,char *pwd,int trial)
  525.  This function is called to get a user name and password for the given
  526. host.  The function stores the user name and password in the strings
  527. pointed to by the appropriate arguments.  The trial argument is the
  528. number of attempts to perform the login and is initially zero (e.g.
  529. for a default username and password login functionality).  It is
  530. incremented for each subsequent trial until the maximum number of
  531. trials are made.  Note that the stream is locked and that therefore
  532. you cannot call any mail_xxx functions!
  533.  
  534. void mm_critical (MAILSTREAM *stream)
  535.  This function is called to alert the application that the c-client is
  536. about to run some critical code that may result in a clobbered mail
  537. file if it is interrupted.  Note that the stream is locked and that
  538. therefore you cannot call any mail_xxx functions!
  539.  
  540. void mm_nocritical (MAILSTREAM *stream)
  541.  This function is called to alert the application that the c-client is
  542. no longer running critical code that may result in a clobbered mail
  543. file if it is interrupted.  Note that the stream is locked and that
  544. therefore you cannot call any mail_xxx functions!
  545.  
  546. int mm_diskerror (MAILSTREAM *stream,int errcode,int serious)
  547.  This function is called to alert the application that the c-client
  548. has encountered an unrecoverable write error when trying to update the
  549. mail file.  errcode contains the system error code.  If serious is
  550. non-zero, then it is probable that the disk copy of the mailbox has
  551. been damaged.  The return value from this function is the abort flag;
  552. if serious is zero and the abort flag is non-zero, the operation is
  553. aborted.  If the abort flag is zero or if serious was non-zero, a
  554. return from this function will retry the failing operation.
  555.  
  556. void mm_fatal (char *string)
  557.  This function is called from the fatal() routine in the operating
  558. system code to notify the main program that it is about to crash.  The
  559. string contains a reason.  At the very minimum, the main program
  560. should do something like
  561.  mm_log (string,ERROR);
  562. and then return.  No newline is included in the string, so this
  563. function has to output its own.
  564.  
  565.             * * * IMPORTANT * * *
  566.  
  567.   Any multi-tasking application should test stream->lock prior to
  568. calling any functions in this module.  Any attempt to do a mail_*
  569. operation while one is already in progress on the same stream will
  570. cause the application to fail in unpredictable ways, mostly likely due
  571. to the _exit() calls in the internal mail_lock() routine when it
  572. discovers the stream is already locked.
  573.  
  574.  Note that this check is insufficient in a preemptive-scheduling
  575. multi-tasking application due to the possibility of a timing race.
  576. Such applications must be written so that only one process accesses
  577. the stream, or to have a higher level lock.
  578.  
  579.  Since MAIL operations will not finish until they are completed, a
  580. single-tasking application does not have to worry about this problem,
  581. except in the co-routines invoked from MAIL (e.g. mm_exists(), etc.)
  582. in which case the stream is *always* locked.
  583.  
  584.  
  585.        III. Operating system-dependent public interface
  586.  
  587.      The files listed below must be rewritten for each port of the
  588. C-Client to a new operating system.
  589.  
  590. [OSDEP.C]:    operating system-dependent functions
  591.  
  592. #include "mail.h"
  593. #include "osdep.h"
  594.  
  595. void rfc822_date (char *date)
  596.  This function is called to get the current date and time in an RFC 822
  597. format string into the the string pointed to by the argument.
  598.  
  599. void *fs_get (int size)
  600.  This function allocates and returns a block of free storage of the specified
  601. size.  Unlike malloc(), there is no failure return.
  602.  
  603. void fs_resize (void **block,int size)
  604.  This function resizes the free storage block, updating the pointer if
  605. necessary.  Unlike realloc(), there is no failure return.
  606.  
  607. void fs_give (void **block)
  608.  This function releases a block of free storage allocated by fs_get and
  609. zeros the block pointer.
  610.  
  611. void fatal (char *string)
  612.  This function is called when an "impossible" error is detected and the
  613. client wishes to crash.  The string argument contains a reason string.
  614.  
  615. char *strcrlfcpy (char **dst,long *dstl,char *src,long srcl)
  616.  This function is called to copy into a destination string dst of size dstl
  617. (resized if necessary), a CRLF newline form string from local string src of
  618. size srcl.
  619.  
  620. TCPSTREAM *tcp_open (char *host,int port)
  621.  This function is called to open a TCP connection to the given host
  622. name on the given port number.  It returns a TCPSTREAM, which is an
  623. internal data structure for this file and is implementation-dependent.
  624. NIL is returned if this function fails for any reason.
  625.  
  626. char *tcp_getline (TCPSTREAM *stream)
  627.  This function returns a malloc() string containing a null-terminated
  628. text line from the TCP stream.  The CR/LF which terminated the line is
  629. not included.  The caller should free() the string when it is finished
  630. with it.  NIL is returned if this function fails for any reason.
  631.  
  632. tcp_getbuffer (TCPSTREAM *stream,int size,char *buffer)
  633.  This function reads the requested number of bytes into the indicated
  634. buffer.  This function returns T if successful and NIL if it fails for
  635. any reason.
  636.  
  637. int tcp_soutr (TCPSTREAM *stream,char *string)
  638.  This function writes the indicated (null-terminated) string to the
  639. TCP stream.  This function returns T if successful and NIL if it fails
  640. for any reason.
  641.  
  642. int tcp_close (TCPSTREAM *stream)
  643.  The function closes the TCP stream and frees all resources associated
  644. with it that it may have created.
  645.  
  646. char *tcp_host (TCPSTREAM *stream)
  647.  This function returns the stream's foreign host name string.
  648.  
  649. char *tcp_localhost (TCPSTREAM *stream)
  650.  This function returns the stream's local host name string.
  651.  
  652.              IV. Driver interface
  653.  
  654.      When writing a new driver for the C-Client, you must provide a
  655. DRIVER stucture giving a dispatch vector between MAIL and the driver.
  656. The DRIVER dispatch vector is described in MAIL.H.
  657.  
  658. [driver.C]:    driver for a particular mailbox type
  659.  
  660. #include "mail.h"
  661. #include "driver.h"
  662. #include "misc.h"
  663. #include "osdep.h"
  664.  
  665. DRIVER *next;
  666.  The first entry is a DRIVER * pointer to the next driver which this
  667. application supports (or NIL if this is the last driver).  Drivers are
  668. lunk together via the mail_link() function.  The remaining entries are
  669. function pointers as follows
  670.  
  671. DRIVER *driver_valid (char *name)
  672.  This function returns a pointer to the driver's DRIVER dispatch
  673. vector iff this driver accepts the given name as a valid mailbox for
  674. this driver.  Otherwise, it returns the value of the next driver's
  675. driver_valid() or NIL if there is no next driver.  In other words,
  676. calling driver_valid() for the first driver will return the driver
  677. dispatch vector for the driver which supports this type of mailbox.
  678.  
  679. void driver_find (MAILSTREAM *stream,char *pat)
  680.  This function implements mail_find() function for this driver.
  681.  
  682. void driver_find_bboard (MAILSTREAM *stream,char *pat);
  683.  This function implements mail_find_bboard() function for this driver.
  684.  
  685. MAILSTREAM *driver_open (MAILSTREAM *stream)
  686.  This function opens the mailbox identified by the given stream.  It
  687. may use the data on the stream and create additional data on
  688. stream->local as necessary.  It should return the given stream unless
  689. it failed to open the mailbox, in which case it should return NIL.
  690.  
  691. void driver_close (MAILSTREAM *stream)
  692.  This function implements mail_close() function for this driver.
  693.  
  694. void driver_fetchfast (MAILSTREAM *stream,char *sequence)
  695.  This function implements mail_fetchfast() function for this driver.
  696.  
  697. void driver_fetchflags (MAILSTREAM *stream,char *sequence)
  698.  This function implements mail_fetchflags() function for this driver.
  699.  
  700. ENVELOPE *driver_fetchenvelope (MAILSTREAM *stream,int msgno)
  701.  This function implements mail_fetchenvelope() function for this
  702. driver.
  703.  
  704. char *driver_fetchheader (MAILSTREAM *stream,int msgno)
  705.  This function implements mail_fetchheader() function for this driver.
  706.  
  707. char *driver_fetchtext (MAILSTREAM *stream,int msgno)
  708.  This function implements mail_fetchtext() function for this driver.
  709.  
  710. char *driver_fetchbody (MAILSTREAM *stream,int msgno,char *section)
  711.  This function implements mail_fetchbody() function for this driver.
  712.  
  713. void driver_setflag (MAILSTREAM *stream,char *sequence,char *flag)
  714.  This function implements mail_setflag() function for this driver.
  715.  
  716. void driver_clearflag (MAILSTREAM *stream,char *sequence,char *flag)
  717.  This function implements mail_clearflag() function for this driver.
  718.  
  719. void driver_search (MAILSTREAM *stream,char *criteria)
  720.  This function implements mail_search() function for this driver.
  721.  
  722. int driver_ping (MAILSTREAM *stream)
  723.  This function implements mail_ping() function for this driver.
  724.  
  725. void driver_check (MAILSTREAM *stream)
  726.  This function implements mail_check() function for this driver.
  727.  
  728. void driver_expunge (MAILSTREAM *stream)
  729.  This function implements mail_expunge() function for this driver.
  730.  
  731. int driver_copy (MAILSTREAM *stream,char *sequence,char *mailbox)
  732.  This function implements mail_copy() function for this driver.
  733.  
  734. int driver_move (MAILSTREAM *stream,char *sequence,char *mailbox)
  735.  This function implements mail_move() function for this driver.
  736.  
  737.      Drivers may call all of the MAIL functions documented above plus
  738. the following co-routine support functions:
  739.  
  740. void mail_searched (MAILSTREAM *stream,int msgno)
  741.  This function is called by the driver to notify MAIL that this
  742. message number matches a search.  It invokes the main program's
  743. mm_searched() function.
  744.  
  745. void mail_exists (MAILSTREAM *stream,int nmsgs)
  746.  This function is called by the driver to notify MAIL that this
  747. message number exists (i.e. there are this many messages in the
  748. mailbox).  It invokes the main program's mm_exists() function.
  749.  
  750. void mail_recent (MAILSTREAM *stream,int recent)
  751.  This function is called by the driver to notify MAIL that this
  752. many messages are "recent" (i.e. arrived in the mailbox since the
  753. previous time the mailbox was opened).
  754.  
  755. void mail_expunged (MAILSTREAM *stream,int msgno)
  756.  This function is called by the driver to notify MAIL that this
  757. message number has been expunged from the mail file and that all
  758. subsequent messages are no references by a message number one less
  759. than before.  It invokes the main program's mm_expunged() function.
  760.  
  761. void mail_lock (MAILSTREAM *stream)
  762.  This function sets the stream lock.  It is an error to set the stream
  763. lock if the stream is already locked.
  764.  This is mainly used to catch errors due to a co-routine function
  765. (e.g. mm_exists) inadvertantly recursing back to the MAIL routines and
  766. establishing an infinite recursion.  Normally, drivers will set the
  767. lock prior to calling one of the co-routine functions above or, more
  768. likely, in the beginning of the driver's non-reentrant "do operation"
  769. section.  In the IMAP2 driver, the stream lock is set when entering
  770. imap_send() and cleared on exit.
  771.  
  772. void mail_unlock (MAILSTREAM *stream)
  773.  This function releases the stream lock.  It is an error to release
  774. the stream lock if the stream is not locked.
  775.